package handler

import (
	"context"
	"server/basic/config"
	"server/basic/model"
	"server/basic/proto/docker"
)

type Server struct {
	docker.UnimplementedDockerServerServer
}

func (s *Server) UserLogin(_ context.Context, req *docker.UserLoginReq) (*docker.UserLoginResp, error) {
	var user model.Users
	err := config.DB.Where("phone = ?", req.Phone).Find(&user).Error
	if err != nil {
		return nil, err
	}
	if user.Id == 0 {
		user.Phone = req.Phone
		user.Password = req.Password
		err = config.DB.Create(&user).Error
		if err != nil {
			return nil, err
		}
	}
	if user.Password != req.Password {
		return nil, err
	}
	return &docker.UserLoginResp{UserId: user.Id}, nil
}

func (s *Server) InstitutionsList(_ context.Context, req *docker.InstitutionsListReq) (*docker.InstitutionsListResp, error) {
	var institutions []model.Institutions
	err := config.DB.Find(&institutions).Error
	if err != nil {
		return nil, err
	}
	inst := []*docker.InstitutionsList{}
	for _, institution := range institutions {
		inst = append(inst, &docker.InstitutionsList{
			Id:                institution.Id,
			Name:              institution.Name,
			Image:             institution.Image,
			BriefIntroduction: institution.BriefIntroduction,
		})
	}

	return &docker.InstitutionsListResp{List: inst}, nil
}

func (s *Server) TeamsList(_ context.Context, req *docker.TeamsListReq) (*docker.TeamsListResp, error) {
	var Teams []model.Teams
	page := 1
	if page <= 0 {
		page = 1
	}

	pageSize := 2
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	config.DB.Offset(offset).Limit(pageSize).Find(&Teams)

	err := config.DB.Where("institution_id=?", req.InstitutionId).Find(&Teams).Error
	if err != nil {
		return nil, err
	}
	inst := []*docker.TeamsList{}
	for _, institution := range Teams {
		inst = append(inst, &docker.TeamsList{
			Id:                institution.Id,
			InstitutionId:     institution.InstitutionId,
			Name:              institution.Name,
			Image:             institution.Image,
			BriefIntroduction: institution.BriefIntroduction,
		})
	}

	return &docker.TeamsListResp{List: inst}, nil
}

func (s *Server) DockerList(_ context.Context, req *docker.DockerListReq) (*docker.DockerListResp, error) {
	var Teams []model.Dockers
	err := config.DB.Where("tema_id=?", req.TeamId).Order("number_of_signatories desc").Find(&Teams).Error
	if err != nil {
		return nil, err
	}
	inst := []*docker.DockerList{}
	for _, institution := range Teams {
		inst = append(inst, &docker.DockerList{
			Id:                  int64(institution.Id),
			TeamId:              int64(institution.TemaId),
			Name:                institution.Name,
			Image:               institution.Image,
			NumberOfSignatories: institution.NumberOfSignatories,
			BriefIntroduction:   institution.BriefIntroduction,
		})
	}

	return &docker.DockerListResp{List: inst}, nil
}

func (s *Server) DockerOne(_ context.Context, req *docker.DockerOneReq) (*docker.DockerOneResp, error) {
	var Teams model.Dockers
	err := config.DB.Where("id=?", req.DockerId).Find(&Teams).Error
	if err != nil {
		return nil, err
	}
	inst := []*docker.DockerList{}
	inst = append(inst, &docker.DockerList{
		Id:                  int64(Teams.Id),
		TeamId:              int64(Teams.TemaId),
		Name:                Teams.Name,
		NumberOfSignatories: Teams.NumberOfSignatories,
		Image:               Teams.Image,
		BriefIntroduction:   Teams.BriefIntroduction,
	})

	return &docker.DockerOneResp{List: inst}, nil
}

func (s *Server) Dockershen(_ context.Context, req *docker.DockershenReq) (*docker.DockershenResp, error) {
	var Teams model.Dockers
	err := config.DB.Where("id=?", req.DockerId).Find(&Teams).Error
	if err != nil {
		return nil, err
	}
	inst := []*docker.DockerList{}
	inst = append(inst, &docker.DockerList{
		Id:                  int64(Teams.Id),
		TeamId:              int64(Teams.TemaId),
		Name:                Teams.Name,
		NumberOfSignatories: Teams.NumberOfSignatories,
		Image:               Teams.Image,
		BriefIntroduction:   Teams.BriefIntroduction,
	})

	return &docker.DockershenResp{Success: true}, nil
}
